package com.alibaba.travel.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.alibaba.travel.common.utils.PageUtils;
import com.alibaba.travel.common.utils.R;
import com.alibaba.travel.member.Utils.UserHolder;
import com.alibaba.travel.member.dto.LoginPhoneInfo;
import com.alibaba.travel.member.dto.LoginUsernameInfo;
import com.alibaba.travel.member.dto.RegisterInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.travel.member.entity.UserEntity;
import com.alibaba.travel.member.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 用户信息表
 *
 * @author wherezy
 * @email wherezy@tom.com
 * @date 2023-02-23 15:05:36
 */

//只要是访问/member/user/路径下的东西，所有请求，除了登陆注册，全部都要加header,加jwt。

@Api(tags = {"用户信息表UserController"})
@RestController
@RequestMapping("member/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("用户注册功能请求接口")
    @PostMapping("/register")
    public R register(@RequestBody RegisterInfo registerInfo) {
        return userService.register(registerInfo);
    }


    @ApiOperation("用户账号密码登录功能请求接口")
    @PostMapping("/login/username")
    public R loginWithUsername(@RequestBody LoginUsernameInfo loginUsernameInfo) {
        return userService.loginWithUsername(loginUsernameInfo);
    }

    @ApiOperation("用户手机号登录功能请求接口")
    @PostMapping("/login/phone")
    public R loginWithPhone(@RequestBody LoginPhoneInfo LoginPhoneInfo) {
        return userService.loginWithPhone(LoginPhoneInfo);
    }



    @ApiOperation("用户发送验证码功能请求接口")
    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody LoginPhoneInfo loginPhoneInfo) {
        return userService.sendMsg(loginPhoneInfo);
    }

    @ApiOperation("用户注销功能请求接口")
    @GetMapping("/logout")
    public R logout() {
        return userService.logout();
    }



    /**
     * 列表
     */
    @ApiOperation(value = "分页获取信息", notes = "k-v map形式封装分页参数")
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "获取信息")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }


    /**
     * 查看个人信息
     */
    @ApiOperation(value = "查看个人信息")
    @GetMapping("/info/me")
    public R me() {
        UserEntity user = UserHolder.getUser();
        if(user==null){
            return R.error("查询失败！");
        }
        return R.ok("查询成功").setData(user);
    }



    /**
     * 保存
     */
    @ApiOperation(value = "保存")
    @PostMapping("/save")
    public R save(@RequestBody UserEntity user) {
        UserEntity u = userService.getById(user.getId());
        if (u != null) return R.error("用户名已存在");
        if (!user.getUserName().matches("/^[\\w\\u4e00-\\u9fa5]{4,16}$/")) return R.error("用户名格式错误");
        if (!user.getPassword().matches("/^.*(?=.{6,16})(?=.*\\d)(?=.*[A-Za-z])(?=.*[!@#$%^&*?\\.]).*$/"))
            return R.error("密码格式错误");
        userService.save(user);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public R update(@RequestBody UserEntity user) {
        //更新redis和数据库。
        return userService.updateDataAndCache(user);

    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }





}
